package com.kingdee.eas.custom.hostelmgr.basedata.app.di;

import java.util.Hashtable;
import java.util.Locale;

import com.kingdee.bos.Context;
import com.kingdee.eas.basedata.person.PersonCollection;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.custom.hostelmgr.basedata.DistributionOrNotEnum;
import com.kingdee.eas.custom.hostelmgr.basedata.HostelBasicFactory;
import com.kingdee.eas.custom.hostelmgr.basedata.HostelBasicInfo;
import com.kingdee.eas.custom.hostelmgr.basedata.NeedRoomEnum;
import com.kingdee.eas.custom.hostelmgr.beddistribution.BedDistributionFactory;
import com.kingdee.eas.custom.hostelmgr.beddistribution.BedDistributionInfo;
import com.kingdee.eas.custom.hostelmgr.beddistribution.IBedDistribution;
import com.kingdee.eas.framework.CoreBaseInfo;
import com.kingdee.eas.framework.ICoreBase;
import com.kingdee.eas.hr.rec.util.DateUtil;
import com.kingdee.eas.tools.datatask.AbstractDataTransmission;
import com.kingdee.eas.tools.datatask.core.TaskExternalException;
import com.kingdee.eas.tools.datatask.runtime.DataToken;
import com.kingdee.eas.util.ResourceBase;
import com.kingdee.jdbc.rowset.IRowSet;

public class PersonInHostelDI extends AbstractDataTransmission {

	protected String RESOURCE_FILE_NAME = "com.kingdee.eas.custom.hostelmgr.basedata.ImportResource";

	@Override
	protected ICoreBase getController(Context ctx) throws TaskExternalException {
		ICoreBase iCoreBase = null;
		try {
			iCoreBase = getBizInterface(ctx);
		} catch (Exception e) {
			throw new TaskExternalException(e.getMessage(), e);
		}

		return iCoreBase;
	}

	public CoreBaseInfo transmit(Hashtable hashtable, Context ctx) throws TaskExternalException {
		try {
			CoreBaseInfo newData = createNewData(hashtable, ctx);
			return newData;
		} catch (Exception e) {
			throw new TaskExternalException(e.getMessage(), e);
		}
	}

	public Hashtable exportTransmit(IRowSet rs, Context ctx) throws TaskExternalException {
		Hashtable result = new Hashtable();
		try {
			result.put("FBuilding_number", avoidNull(rs.getString("building.number")));
			result.put("FUnit_number", avoidNull(rs.getString("unit.number")));
			result.put("FFloor_number", avoidNull(rs.getString("floor.number")));
			result.put("FRoom_number", avoidNull(rs.getString("room.number")));
			result.put("FBed_number", avoidNull(rs.getString("bed.number")));
			result.put("FPerson_number", avoidNull(rs.getString("person.number")));
			result.put("FMobile", avoidNull(rs.getString("mobile")));
			result.put("FStayDate", avoidNull(rs.getString("statyDate")));
			result.put("FRemark", avoidNull(rs.getString("remark")));
		} catch (Exception e) {
			e.printStackTrace();
			Locale currentLocale = ctx.getLocale();
			String message = ResourceBase.getString(this.RESOURCE_FILE_NAME, "DB_ACCESS_ERROR", currentLocale);

			throw new TaskExternalException(message, e);
		}

		return result;
	}

	protected IBedDistribution getBizInterface(Context ctx) throws Exception {
		return BedDistributionFactory.getLocalInstance(ctx);
	}

	protected CoreBaseInfo createNewData(Hashtable hsData, Context ctx) throws Exception {
		String building_number = this.getFieldValue(hsData, "FBuilding_number");
		String unit_number = this.getFieldValue(hsData, "FUnit_number");
		String floor_number = this.getFieldValue(hsData, "FFloor_number");
		String room_number = this.getFieldValue(hsData, "FRoom_number");
		String bed_number = this.getFieldValue(hsData, "FBed_number");
		String person_number = this.getFieldValue(hsData, "FPerson_number");
		String mobile = this.getFieldValue(hsData, "FMobile");
		String staydate = this.getFieldValue(hsData, "FStayDate");
		String remark = this.getFieldValue(hsData, "FRemark");

		this.checkRequeiredField(ctx, "FBuilding_number", building_number);
		this.checkRequeiredField(ctx, "FUnit_number", unit_number);
		this.checkRequeiredField(ctx, "FFloor_number", floor_number);
		this.checkRequeiredField(ctx, "FRoom_number", room_number);
		this.checkRequeiredField(ctx, "FBed_number", bed_number);
		this.checkRequeiredField(ctx, "FPerson_number", person_number);
		this.checkRequeiredField(ctx, "FStayDate", staydate);

		String longNumber = building_number + "!" + unit_number + "!" + floor_number + "!" + room_number + "!"
				+ bed_number;

		HostelBasicInfo bedInfo = this.getHostelBasicInfoByLongNum(ctx, longNumber);
		PersonInfo personInfo = this.getPersonInfoByNumber(ctx, person_number);
		personInfo.setIsDistribution(DistributionOrNotEnum.YES);

		BedDistributionInfo bedDistributionInfo = this.getBizInterface(ctx).getBedDistributionInfo(
				"where bed.id='" + bedInfo.getId().toString() + "'");
//		if (bedDistributionInfo.getPerson() != null) {
//			StringBuffer message = new StringBuffer();
//			Locale currentLocale = ctx.getLocale();
//			message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME, "BED", currentLocale));
//			message.append(longNumber);
//			message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME, "IS_DISTRIBUTION", currentLocale));
//			throw new Exception(message.toString());
//		}

		bedDistributionInfo.setPerson(personInfo);
		bedDistributionInfo.setBed(bedInfo);
		bedDistributionInfo.setStayDate(DateUtil.parse(staydate, "yyyy-MM-dd"));
		bedDistributionInfo.setMobile(mobile);
		bedDistributionInfo.setRemark(remark);

		return bedDistributionInfo;
	}

	protected void checkRequeiredField(Context ctx, String field, String value) throws Exception {
		if ((value == null) || (value.trim().length() == 0)) {
			StringBuffer message = new StringBuffer();
			Locale currentLocale = ctx.getLocale();
			message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME, "FIELD", currentLocale));
			message.append(field);
			message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME, "NOT_NULL", currentLocale));

			throw new Exception(message.toString());
		}
	}

	protected HostelBasicInfo getHostelBasicInfoByLongNum(Context ctx, String longNumber) throws Exception {
		HostelBasicInfo bedInfo = HostelBasicFactory.getLocalInstance(ctx).getHostelBasicInfo(
				"where longNumber='" + longNumber + "'");

		if (bedInfo == null) {
			StringBuffer message = new StringBuffer();
			Locale currentLocale = ctx.getLocale();
			message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME, "BED", currentLocale));
			message.append(longNumber);
			message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME, "NOT_FOUND", currentLocale));
			throw new Exception(message.toString());
		}

		return bedInfo;
	}

	protected PersonInfo getPersonInfoByNumber(Context ctx, String number) throws Exception {
		PersonCollection collection = PersonFactory.getLocalInstance(ctx).getPersonCollection(
				"where number='" + number + "'");

		if (collection.size() > 0) {
			PersonInfo person = collection.get(0);
			if (person.getIsRoom().equals(NeedRoomEnum.YES)
					&& person.getIsDistribution().equals(DistributionOrNotEnum.NO)) {
				return person;
			} else if (person.getIsRoom().equals(NeedRoomEnum.NO)) {
				StringBuffer message = new StringBuffer();
				Locale currentLocale = ctx.getLocale();
				message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME, "PERSON", currentLocale));
				message.append(person.getNumber());
				message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME, "NOT_NEED_ROOM", currentLocale));

				throw new Exception(message.toString());
			} else if (person.getIsDistribution().equals(DistributionOrNotEnum.NO)) {
				StringBuffer message = new StringBuffer();
				Locale currentLocale = ctx.getLocale();
				message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME, "PERSON", currentLocale));
				message.append(person.getNumber());
				message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME, "IS_DISTRIBUTION", currentLocale));
				throw new Exception(message.toString());
			}
		} else {
			StringBuffer message = new StringBuffer();
			Locale currentLocale = ctx.getLocale();
			message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME, "PERSON", currentLocale));
			message.append(number);
			message.append(ResourceBase.getString(this.RESOURCE_FILE_NAME, "NOT_FOUND", currentLocale));
			throw new Exception(message.toString());
		}

		return null;
	}

	protected String getFieldValue(Hashtable hsData, String key) {
		return ((String) ((DataToken) hsData.get(key)).data).trim();
	}

	protected String avoidNull(Object obj) {
		if (obj != null) {
			return obj.toString();
		}
		return "";
	}
}
